package com.vhall.component.service.filter.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.filter.FilterWordsLogMapper;
import com.vhall.component.dao.filter.FilterWordsMapper;
import com.vhall.component.entity.filter.FilterWordsEntity;
import com.vhall.component.entity.filter.FilterWordsLogEntity;
import com.vhall.component.entity.filter.vo.FilterWordsGetReqVO;
import com.vhall.component.entity.filter.vo.FilterWordsReportReqVO;
import com.vhall.component.entity.room.dto.RoomsEntityDTO;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.constant.RedisKey;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.filter.FilterWordsApiService;
import com.vhall.component.service.room.RoomInnerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author longzhen.gong
 * @date 2021/7/2 15:32
 */
@Service
public class FilterWordsApiServiceImpl implements FilterWordsApiService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private RoomInnerService RoomInnerService;


    @Autowired
    private FilterWordsMapper filterWordsMapper;

    @Autowired
    private FilterWordsLogMapper filterWordsLogMapper;

    /**
     * 获取敏感词
     *
     * @param reqVO
     * @return
     */
    @Override
    public String getFilterWords(FilterWordsGetReqVO reqVO) {
        String words = redisTemplate.opsForValue().get(RedisKey.FILTER_WORDS_CACHE_KEY + 1);
        if (StringUtils.isBlank(words)) {
            RoomsEntityDTO roomsEntityDTO = RoomInnerService.getRoomInfo(reqVO.getRoomId());
            if (roomsEntityDTO == null) {
                throw new BusinessException(BizErrorCode.EMPTY_ROOM);
            }

            List<FilterWordsEntity> wordsEntities = filterWordsMapper.selectList(Wrappers.<FilterWordsEntity>lambdaQuery()
//                    .eq(FilterWordsEntity::getAccountId, roomsEntityDTO.getAccountId())
//                    .in(FilterWordsEntity::getIlId, roomsEntityDTO.getIlId(), 0)
//                    .eq(FilterWordsEntity::getIlId, roomsEntityDTO.getIlId())
//                    .select(FilterWordsEntity::getKeyword)
//                            .and(wrapper -> wrapper.eq(FilterWordsEntity::getAccountId, 0))
//                            .or(wrapper -> wrapper.eq(FilterWordsEntity::getAccountId, roomsEntityDTO.getAccountId()).eq(FilterWordsEntity::getIlId, 0).isNull(FilterWordsEntity::getRoomId))
//                            .or(wrapper -> wrapper.eq(FilterWordsEntity::getRoomId, reqVO.getRoomId()).eq(FilterWordsEntity::getAccountId, roomsEntityDTO.getAccountId()).eq(FilterWordsEntity::getIlId, roomsEntityDTO.getIlId()))
//                            .select(FilterWordsEntity::getKeyword)
            );
            words = wordsEntities.stream().map(FilterWordsEntity::getKeyword).collect(Collectors.joining(","));
            redisTemplate.opsForValue().set(RedisKey.FILTER_WORDS_CACHE_KEY + 1, words, 86400, TimeUnit.SECONDS);
            redisTemplate.opsForSet().add(RedisKey.FILTER_WORDS_ILID_SET, reqVO.getRoomId());
        }
        return words;
    }

    /**
     * @param reqVO
     * @param accountId
     * @return
     */
    @Override
    public Boolean reportFilterWords(FilterWordsReportReqVO reqVO, Integer accountId) {
        RoomsEntityDTO roomsEntityDTO = RoomInnerService.getRoomInfo(reqVO.getRoomId());
        if (roomsEntityDTO == null) {
            throw new BusinessException(BizErrorCode.EMPTY_ROOM);
        }
        FilterWordsLogEntity filterWordsLogEntity = new FilterWordsLogEntity();
        filterWordsLogEntity.setAccountId(accountId);
        filterWordsLogEntity.setContent(reqVO.getKeyword());
        filterWordsLogEntity.setLiveStatus(roomsEntityDTO.getStatus());
        filterWordsLogEntity.setIlId(roomsEntityDTO.getIlId());
        filterWordsLogEntity.setRoomId(roomsEntityDTO.getRoomId());
        return filterWordsLogMapper.insert(filterWordsLogEntity) > 0;
    }
}
